package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.VipUserBalanceDetailBo;
import com.rla.domain.vo.VipUserBalanceDetailVo;
import com.rla.domain.VipUserBalanceDetail;
import com.rla.mapper.VipUserBalanceDetailMapper;
import com.rla.service.IVipUserBalanceDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户储值记录明细Service业务层处理
 *
 * @author rla
 * @date 2025-08-18
 */
@RequiredArgsConstructor
@Service
public class VipUserBalanceDetailServiceImpl implements IVipUserBalanceDetailService {

    private final VipUserBalanceDetailMapper baseMapper;

    /**
     * 查询用户储值记录明细
     */
    @Override
    public VipUserBalanceDetailVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户储值记录明细列表
     */
    @Override
    public TableDataInfo<VipUserBalanceDetailVo> queryPageList(VipUserBalanceDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VipUserBalanceDetail> lqw = buildQueryWrapper(bo);
        Page<VipUserBalanceDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户储值记录明细列表
     */
    @Override
    public List<VipUserBalanceDetailVo> queryList(VipUserBalanceDetailBo bo) {
        LambdaQueryWrapper<VipUserBalanceDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VipUserBalanceDetail> buildQueryWrapper(VipUserBalanceDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VipUserBalanceDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUserId()), VipUserBalanceDetail::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getBalanceType()), VipUserBalanceDetail::getBalanceType, bo.getBalanceType());
        lqw.eq(bo.getChangeAmount() != null, VipUserBalanceDetail::getChangeAmount, bo.getChangeAmount());
        lqw.eq(bo.getChangeTime() != null, VipUserBalanceDetail::getChangeTime, bo.getChangeTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDetailCode()), VipUserBalanceDetail::getDetailCode, bo.getDetailCode());
        lqw.eq(bo.getFgcCreatedate() != null, VipUserBalanceDetail::getFgcCreatedate, bo.getFgcCreatedate());
        lqw.eq(StringUtils.isNotBlank(bo.getFgcLastmodifier()), VipUserBalanceDetail::getFgcLastmodifier, bo.getFgcLastmodifier());
        lqw.eq(bo.getFgcLastmodifydate() != null, VipUserBalanceDetail::getFgcLastmodifydate, bo.getFgcLastmodifydate());
        lqw.eq(StringUtils.isNotBlank(bo.getFgcCreator()), VipUserBalanceDetail::getFgcCreator, bo.getFgcCreator());
        return lqw;
    }

    /**
     * 新增用户储值记录明细
     */
    @Override
    public Boolean insertByBo(VipUserBalanceDetailBo bo) {
        VipUserBalanceDetail add = BeanUtil.toBean(bo, VipUserBalanceDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户储值记录明细
     */
    @Override
    public Boolean updateByBo(VipUserBalanceDetailBo bo) {
        VipUserBalanceDetail update = BeanUtil.toBean(bo, VipUserBalanceDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(VipUserBalanceDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户储值记录明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
